package com.utooo.service.supplier;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.utooo.dao.purchase.*;
import com.utooo.dao.purchase.contract.PurchaseContractRenewalMapper;
import com.utooo.dao.purchase.contract.SupplierPurchaseContractRenewalMapper;
import com.utooo.pojo.domain.purchase.contract.*;
import com.utooo.pojo.dto.supplier.purchase.PurchaseContractRenewDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.utooo.constant.Constans.IGNORED_PROPERTIES;

/**
 * @Description: 供货方合同续签
 * @Author SJD
 * @Date: 2025/4/29 13:41
 * @Copyright: 南京优通信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class SupplierPurchaseServiceImpl implements SupplierPurchaseService{

    private final SupplierPurchaseContractRenewalMapper supplierPurchaseContractRenewalMapper;
    private final PurchaseContractRenewalMapper purchaseContractRenewalMapper;
    private final ContractPartyAMapper contractPartyAMapper;
    private final ContractPartyBMapper contractPartyBMapper;
    private final ThirdPartyMapper thirdPartyMapper;
    private final ContractObjectMapper contractObjectMapper;
    private final ContractAgreementsStageMapper agreementsStageMapper;
    private final ContractAttachmentMapper attachmentMapper;

    @Override
    @Transactional
    public Boolean pushPurchaseContractRenew(PurchaseContractRenewDto param) {
        SupplierPurchaseContractRenewalDO contractRenewalDO = supplierPurchaseContractRenewalMapper.selectOne(new LambdaQueryWrapper<SupplierPurchaseContractRenewalDO>()
                .eq(SupplierPurchaseContractRenewalDO::getContractNo, param.getContractNo()));
        PurchaseContractRenewalDO purchaseContractRenewalDO = BeanUtil.copyProperties(contractRenewalDO, PurchaseContractRenewalDO.class, IGNORED_PROPERTIES);
        purchaseContractRenewalMapper.insert(purchaseContractRenewalDO);

        //合同签署方
        List<ContractPartyADO> contractPartyADOS = contractPartyAMapper.selectList(new LambdaQueryWrapper<ContractPartyADO>().eq(ContractPartyADO::getForeignId, contractRenewalDO.getId()));
        if(contractPartyADOS!= null && !contractPartyADOS.isEmpty()){
            contractPartyADOS.forEach(contractPartyADO -> {
                contractPartyADO.setForeignId(purchaseContractRenewalDO.getId());
                contractPartyADO.setCreateTime(LocalDateTime.now());
            });
            contractPartyAMapper.insert(contractPartyADOS);
        }
        //合同签署乙方
        List<ContractPartyBDO> contractPartyBDOS = contractPartyBMapper.selectList(new LambdaQueryWrapper<ContractPartyBDO>().eq(ContractPartyBDO::getForeignId, contractRenewalDO.getId()));
        if(contractPartyBDOS!= null && !contractPartyBDOS.isEmpty()){
            contractPartyBDOS.forEach(contractPartyBDO -> {
                contractPartyBDO.setForeignId(purchaseContractRenewalDO.getId());
                contractPartyBDO.setCreateTime(LocalDateTime.now());
            });
            contractPartyBMapper.insert(contractPartyBDOS);
        }
        //合同签署第三方
        List<ThirdPartyDO> thirdPartyDOS = thirdPartyMapper.selectList(new LambdaQueryWrapper<ThirdPartyDO>().eq(ThirdPartyDO::getForeignId, contractRenewalDO.getId()));
        if(thirdPartyDOS!= null && !thirdPartyDOS.isEmpty()){
            thirdPartyDOS.forEach(thirdPartyDO -> {
                thirdPartyDO.setForeignId(purchaseContractRenewalDO.getId());
                thirdPartyDO.setCreateTime(LocalDateTime.now());
            });
            thirdPartyMapper.insert(thirdPartyDOS);
        }
        //合同标的
        List<ContractObjectDO> contractObjectDOS = contractObjectMapper.selectList(new LambdaQueryWrapper<ContractObjectDO>().eq(ContractObjectDO::getForeignId, contractRenewalDO.getId()));
        if(contractObjectDOS!= null && !contractObjectDOS.isEmpty()){
            contractObjectDOS.forEach(contractObjectDO -> {
                contractObjectDO.setForeignId(purchaseContractRenewalDO.getId());
                contractObjectDO.setCreateTime(LocalDateTime.now());
            });
            contractObjectMapper.insert(contractObjectDOS);
        }
        //合同约定阶段
        List<ContractAgreementsStageDO> agreementsStageDOS = agreementsStageMapper.selectList(new LambdaQueryWrapper<ContractAgreementsStageDO>().eq(ContractAgreementsStageDO::getForeignId, contractRenewalDO.getId()));
        if(agreementsStageDOS!= null && !agreementsStageDOS.isEmpty()){
            agreementsStageDOS.forEach(agreementsStageDO -> {
                agreementsStageDO.setForeignId(purchaseContractRenewalDO.getId());
                agreementsStageDO.setCreateTime(LocalDateTime.now());
            });
            agreementsStageMapper.insert(agreementsStageDOS);
        }
        //合同信息
        List<ContractAttachmentDO> attachmentDOS = attachmentMapper.selectList(new LambdaQueryWrapper<ContractAttachmentDO>().eq(ContractAttachmentDO::getForeignId, contractRenewalDO.getId()));
        if(attachmentDOS!= null && !attachmentDOS.isEmpty()){
            attachmentDOS.forEach(attachmentDO -> {
                attachmentDO.setForeignId(purchaseContractRenewalDO.getId());
                attachmentDO.setCreateTime(LocalDateTime.now());
            });
            attachmentMapper.insert(attachmentDOS);
        }
        return true;
    }
}
